// ## 
#include "ModelGenerator.h"
#include "AtomicGenerators.h"
#include "Slices.h"
// ## C++
#include <cassert>

namespace jm4bbx {

	const char* ModelGenerator::END_BOX_NAME = "output_box";

	const char* ModelGenerator::START_BOX_NAME = "input_box";


	int ModelIdGenerator::getNextBoxId() {	
		return ++boxId_;
	}

	int ModelIdGenerator::getNextViaId() {
		return ++viaId_;
	}

	void ModelIdGenerator::resetGenerator() {
		boxId_ = 0;
		viaId_ = 0;
	}


	ModelGenerator::ModelGenerator() {
		//idGenerator_.resetGenerator();
		addBox(START_BOX_NAME);
		addBox(END_BOX_NAME);
	}



	jm4bbx::BoxDescriptorPtr ModelGenerator::addBox(const std::string& boxName,
		const std::string& boxParams, int repetition /*= 1*/) {
			BoxDescriptorPtr boxDesc(new BoxDescriptor(boxName, boxParams));
			addBox(boxDesc, repetition);
			return boxDesc;
	}

	jm4bbx::BoxDescriptorPtr ModelGenerator::addBox(const std::string& boxName,
		int repetition /*= 1*/) {
			return addBox(boxName, "", repetition);
	}

	void ModelGenerator::addBox(const BoxDescriptorPtr& box, 
		int repetition /*= 1*/) {
			for (int i = 0; i < repetition; ++ i) {
				box->addBoxId(idGenerator_.getNextBoxId());
			}
			boxes_.push_back(box);
	}

	std::string ModelGenerator::generateModel(const std::string& lineEnd) {
		std::stringstream model;

		BOBOX_MODEL_LINE_END = lineEnd;

		for (size_t i = 0; i < boxes_.size(); ++i) {
			model << genBox(boxes_[i]);
		}


		for (size_t i = 0; i < slices_.size(); ++i) {
			model << slices_[i]->generateBody(idGenerator_);
		}

		return model.str();
	}

	int ModelGenerator::getModelInputId() const {
		assert(boxes_.size() >= 2);
		return boxes_[0]->getBoxId();
	}

	int ModelGenerator::getModelOutputId() const {
		assert(boxes_.size() >= 2);
		return boxes_[1]->getBoxId();
	}

} // namespacfe jm4bbx 
